import axios from 'axios';
// Actions
const DUBBO_INIT_INTERFACE_DATA = 'yapi/dubbo_interface/INIT_INTERFACE_DATA';
const DUBBO_FETCH_INTERFACE_DATA = 'yapi/dubbo_interface/FETCH_INTERFACE_DATA';
const DUBBO_FETCH_INTERFACE_LIST_MENU = 'yapi/dubbo_interface/FETCH_INTERFACE_LIST_MENU';
const DUBBO_DELETE_INTERFACE_DATA = 'yapi/dubbo_interface/DELETE_INTERFACE_DATA';
const DUBBO_DELETE_INTERFACE_CAT_DATA = 'yapi/dubbo_interface/DELETE_INTERFACE_CAT_DATA';
const DUBBO_UPDATE_INTERFACE_DATA = 'yapi/dubbo_interface/UPDATE_INTERFACE_DATA';
const DUBBO_CHANGE_EDIT_STATUS = 'yapi/dubbo_interface/CHANGE_EDIT_STATUS';
const DUBBO_FETCH_INTERFACE_LIST = 'yapi/dubbo_interface/FETCH_INTERFACE_LIST';
const DUBBO_SAVE_IMPORT_DATA = 'yapi/dubbo_interface/SAVE_IMPORT_DATA';
const DUBBO_FETCH_INTERFACE_CAT_LIST = 'yapi/dubbo_interface/FETCH_INTERFACE_CAT_LIST';
// const SAVE_INTERFACE_PROJECT_ID = 'yapi/interface/SAVE_INTERFACE_PROJECT_ID';
// const GET_INTERFACE_GROUP_LIST = 'yapi/interface/GET_INTERFACE_GROUP_LIST';
const DUBBO_FETCH_INTERFACE_INFO = 'yapi/dubbo_interface/FETCH_INTERFACE_INFO';
const DUBBO_GET_TXT = 'yapi/dubbo_interface/DUBBO_GET_TXT';

// Reducer
const initialState = {
    // TTC ADD
    infBaseInfo:{}, //接口的基本信息
    methodList:[],  //接口所属的全部方法列表
    // TTC ADD ^^^^
    curdata: {},
    list: [],
    editStatus: false, // 记录编辑页面是否有编辑,
    totalTableList: [],
    catTableList: [],
    count: 0,
    totalCount: 0
};

export default (state = initialState, action) => {
    switch (action.type) {
        case DUBBO_GET_TXT:
            return state;
        case DUBBO_INIT_INTERFACE_DATA:
            return initialState;
        case DUBBO_UPDATE_INTERFACE_DATA:
            return {
                ...state,
                curdata: Object.assign({}, state.curdata, action.updata)
            };
        case DUBBO_FETCH_INTERFACE_DATA:
            return {
                ...state,
                curdata: action.payload.data.data
            };
        //    TTC ADD
        case DUBBO_FETCH_INTERFACE_INFO:
            return {
                ...state,
                infBaseInfo: action.payload.data.data.infBaseInfo,
                methodList: action.payload.data.data.methodList
            };
        // TTC ADD ^^^^
        case DUBBO_FETCH_INTERFACE_LIST_MENU:
            return {
                ...state,
                list: action.payload.data.data
            };
        case DUBBO_CHANGE_EDIT_STATUS: {
            return {
                ...state,
                editStatus: action.status
            };
        }

        case DUBBO_FETCH_INTERFACE_LIST: {
            return {
                ...state,
                totalTableList: (!action.payload.data.data ? [] : action.payload.data.data.list),
                totalCount: (!action.payload.data.data ? 0 : action.payload.data.data.count)
                // totalTableList: action.payload.data.data.list,
                // totalCount: action.payload.data.data.count
            };
        }

        case DUBBO_FETCH_INTERFACE_CAT_LIST: {
            return {
                ...state,
                catTableList: action.payload.data.data.list,
                count: action.payload.data.data.count
            };
        }
        default:
            return state;
    }
};

// 记录编辑页面是否有编辑
export function changeEditStatus(status) {
    return {
        type: DUBBO_CHANGE_EDIT_STATUS,
        status
    };
}

export function initInterface() {
    return {
        type: DUBBO_INIT_INTERFACE_DATA
    };
}

export function updateInterfaceData(updata) {
    return {
        type: DUBBO_UPDATE_INTERFACE_DATA,
        updata: updata,
        payload: true
    };
}

export async function deleteInterfaceData(id) {
    // let result = await axios.post('/api/dubbo_interface/del', { id: id });
    let result = await axios.post('/api/dubbo_interface/del_interface', { id: id });
    return {
        type: DUBBO_DELETE_INTERFACE_DATA,
        payload: result
    };
}

export async function saveImportData(data) {
    let result = await axios.post('/api/dubbo_interface/save', data);
    return {
        type: DUBBO_SAVE_IMPORT_DATA,
        payload: result
    };
}

export async function deleteInterfaceCatData(id) {
    let result = await axios.post('/api/dubbo_interface/del_cat', { catid: id });
    return {
        type: DUBBO_DELETE_INTERFACE_CAT_DATA,
        payload: result
    };
}

// Action Creators
export async function fetchInterfaceData(interfaceId) {
    let result = await axios.get('/api/dubbo_interface/get?id=' + interfaceId);
    return {
        type: DUBBO_FETCH_INTERFACE_DATA,
        payload: result
    };
}

// export async function fetchInterfaceListMenu() {
//     // let result = await axios.get('/api/interface/list_menu?project_id=' + projectId);
//     return {
//         type: FETCH_INTERFACE_LIST_MENU,
//         payload: {
//           data:
//                 {
//                   data:[{add_time: 1566822651,
//                       desc: "公共分类",
//                       index: 0,
//                       list: [],
//                       name: "公共分类",
//                       project_id: 93,
//                       uid: 11,
//                       up_time: 1566822651,
//                       __v: 0,
//                       _id: 111}]
//                 }
//         }
//     };
// }
export async function fetchInterfaceListMenu(projectId) {

    // TTC PUSH TEST ***
    // let result = await axios.get('/api/dubbo_interface/get?projectid=' + projectId);
    let result = await axios.get('/api/dubbo_interface/list_menu?projectid=' + projectId);

    return {
        type: DUBBO_FETCH_INTERFACE_LIST_MENU,
        payload: result
    };
}

export async function fetchInterfaceList(params) {
    let result = await axios.get('/api/dubbo_interface/list', { params });
    return {
        type: DUBBO_FETCH_INTERFACE_LIST,
        payload: result
    };
}

export async function fetchInterfaceCatList(params) {
    let result = await axios.get('/api/dubbo_interface/list_cat', { params });
    return {
        type: DUBBO_FETCH_INTERFACE_CAT_LIST,
        payload: result
    };
}


//  InterfaceInfo新增

export async function fetchInterfaceInfo(infid) {
    let result = await axios.get('/api/dubbo_interface/get_info?_id=' + infid);
    return {
        // type: DUBBO_FETCH_INTERFACE_DATA,
        type: DUBBO_FETCH_INTERFACE_INFO,
        payload: result
    };

}

export async function getTxtAction(ids, pid) {
    let result = {
        ids: ids,
        pid: pid
    }
    return {
        type: DUBBO_GET_TXT,
        payload: await axios.post('/api/dubbo_interface/getTxt', result)
    };
}

export async function fetchMethodList() {

}

//  ^^^^^

// InterfaceSearch新增 根据条件查询接口列表
export async function fetchInterfaceListByCondition(projectid,onlinedate,status,classname) {
    let condition = {
        projectid:projectid,
        onlinedate:onlinedate,
        status:status,
        classname:classname
    };
    let result = await axios.post('/api/dubbo_interface/getInfByCondition',condition);
    return {
        type:DUBBO_FETCH_INTERFACE_LIST,
        payload:result
    };
}